package com.zhuang.chapter08.work01;

import java.util.ArrayList;
import java.util.Scanner;

@SuppressWarnings("all")
public class SchoolManagerSystem {
	public static Scanner sc = new Scanner(System.in);
	// 创建集合对象，用于存储学生数据
	static ArrayList<Student> array = new ArrayList<Student>();
	// 创建集合对象，用于存储老师数据
	static ArrayList<Teacher> teacherArray = new ArrayList<Teacher>();

	public static void main(String[] args) {
		// 用Scanner实现键盘录入数据
		boolean flag = true;
		if (flag) {
			System.out.println("----欢迎来到学校人事管理系统,请先登录----");
			// 用循环完成再次回到主界面
			while (true) {
				// 用输出语句完成主界面的编写
				// 1,庄康,男,2001年,800,1,计算机
				// 1,庄康,男,2001年,北京,经理,3400.00
				System.out.println("请根据对应序号进行选择");
				System.out.println("1 添加信息");
				System.out.println("2 删除信息");
				System.out.println("3 修改信息");
				System.out.println("4 查看信息");
				System.out.println("5 退出");
				System.out.println("请输入你的选择：");
				String line = sc.nextLine();
				// 用switch语句完成操作的选择
				switch (line) {
				case "1":
					chooseAdd(array, teacherArray);
					break;
				case "2":
					chooseDelete(array, teacherArray);
					break;
				case "3":
					chooseUpdate(array, teacherArray);
					break;
				case "4":
					chooseFindAll(array, teacherArray);
					break;
				case "5":
					System.out.println("谢谢使用");
					System.exit(0); // JVM退出
				default:
					System.out.println("没有对应的操作,请重新选择");
				}
			}
		}
	}

	public static void chooseAdd(ArrayList<Student> array, ArrayList<Teacher> teacherArray) {
		Scanner sc = new Scanner(System.in);
		System.out.println("输入添加的类别 1 添加学生 2 添加老师");
		if (sc.nextInt() == 1) {
			addStudent(array);
		} else {
			addTeacher(teacherArray);
		}
	}

	public static void chooseDelete(ArrayList<Student> array, ArrayList<Teacher> teacherArray) {
		Scanner sc = new Scanner(System.in);
		System.out.println("输入删除的类别 1 删除学生 2 删除老师");
		if (sc.nextInt() == 1) {
			deleteStudent(array);
		} else {
			deleteTeacher(teacherArray);
		}
	}

	public static void chooseUpdate(ArrayList<Student> array, ArrayList<Teacher> teacherArray) {
		Scanner sc = new Scanner(System.in);
		System.out.println("输入更新的类别 1 更新学生 2 更新老师");
		if (sc.nextInt() == 1) {
			updateStudent(array);
		} else {
			updateTeacher(teacherArray);
		}

	}

	public static void chooseFindAll(ArrayList<Student> array, ArrayList<Teacher> teacherArray) {
		Scanner sc = new Scanner(System.in);
		System.out.println("输入查找的类别 1 查找学生 2 查找老师");
		if (sc.nextInt() == 1) {
			findAllStudent(array);
		} else {
			findAllTeacher(teacherArray);
		}
	}

	// 定义一个方法，用于修改学生信息
	public static void updateStudent(ArrayList<Student> array) {
		if (array == null || array.size() == 0) {
			System.out.println("没有数据,请先添加再进行修改");
			return;
		}
		// 调用查询所有
		findAllStudent(array);
		// 键盘录入要修改的学生学号，显示提示信息
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你要修改的学生的学号：");
		int id = sc.nextInt();
		boolean b = false;
		// 遍历集合修改对应的学生信息
		for (int i = 0; i < array.size(); i++) {
			Student student = array.get(i);
			if (student.getId() == id) {
				student.show();
				System.out.println("输入学生信息并用,分隔: 编号,姓名,性别,生日,分数,班级,专业");
				Scanner sc2 = new Scanner(System.in);

				String[] strings = sc2.nextLine().split(",");
				// 键盘录入要修改的学生信息
				int updateId = Integer.parseInt(strings[0]);
				String name = strings[1];
				String gender = strings[2];
				String bir = strings[3];
				int grade = Integer.parseInt(strings[4]);
				int classes = Integer.parseInt(strings[5]);
				String pro = strings[6];
				Student newStudent = new Student(updateId, name, gender, bir, grade, classes, pro);
				array.set(i, newStudent);
				// 给出修改成功提示
				System.out.println("修改学生成功");
				b = true;
				break;
			}
		}

		if (b) {
			// 调用查询所有方法
			findAllStudent(array);
			System.out.println("----------------------------------------");
			System.out.println("----------------------------------------");
		} else {
			System.out.println("您要修改的学员不存在!");
			System.out.println("----------------------------------------");
			System.out.println("----------------------------------------");
		}
	}

	public static void updateTeacher(ArrayList<Teacher> teacherArray) {
		if (teacherArray == null || teacherArray.size() == 0) {
			System.out.println("没有数据,请先添加再进行修改");
			return;
		}
		// 调用查询所有
		findAllTeacher(teacherArray);
		// 键盘录入要修改的教师编号，显示提示信息
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你要修改的教师的编号：");
		int id = sc.nextInt();
		boolean b = false;
		// 遍历集合修改对应的学生信息
		for (int i = 0; i < teacherArray.size(); i++) {
			Teacher teacher = teacherArray.get(i);
			teacher.show();
			if (teacher.getId() == id) {
				System.out.println("输入教师信息并用,分隔: 编号,姓名,性别,生日,部门,职称,工资");
				Scanner sc2 = new Scanner(System.in);
				String[] strings = sc2.nextLine().split(",");
				// 键盘录入要修改的教师信息
				String name = strings[1];
				String gender = strings[2];
				String bir = strings[3];
				String department = strings[4];
				String job = strings[5];
				double salary = Double.parseDouble(strings[6]);
				Teacher newTeacher = new Teacher(id, name, gender, bir, department, job, salary);
				teacherArray.set(i, newTeacher);
				// 给出修改成功提示
				System.out.println("修改老师成功");
				b = true;
				break;
			}
		}

		if (b) {
			// 调用查询所有方法
			findAllTeacher(teacherArray);
			System.out.println("----------------------------------------");
			System.out.println("----------------------------------------");
		} else {
			System.out.println("您要修改的教师不存在!");
			System.out.println("----------------------------------------");
			System.out.println("----------------------------------------");
		}

	}

	// 定义一个方法，用于删除学生信息
	public static void deleteStudent(ArrayList<Student> array) {
		if (!checkStudent(array)) {
			System.out.println("----------------------------------------");
			System.out.println("------------无数据,请先添加-------------");
			System.out.println("----------------------------------------");
			return;
		}
		findAllStudent(array);
		// 键盘录入要删除的学生学号,显示提示信息
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你要删除的学生的学号：");
		int id = sc.nextInt();
		// 在删除/修改学生操作前，对学号是否存在进行判断
		// 如果不存在，显示提示信息
		// 如果存在，执行删除/修改操作
		int index = -1;
		for (int i = 0; i < array.size(); i++) {
			Student s = array.get(i);
			if (s.getId() == id) {
				// int index = i;
				index = i; // 把集合的索引赋值给 变量
				break;
			}
		}
		if (index == -1) {
			System.out.println("该信息不存在，请重新输入");
		} else {
			array.remove(index); // 根据索引删除集合的元素
			// 给出删除成功提示
			System.out.println("删除学生成功");
			findAllStudent(array);
			System.out.println("----------------------------------------");
			System.out.println("----------------------------------------");
		}
	}

	public static void deleteTeacher(ArrayList<Teacher> teacherArray) {

		if (!checkTeacher(teacherArray)) {
			System.out.println("----------------------------------------");
			System.out.println("------------无数据,请先添加-------------");
			System.out.println("----------------------------------------");
			return;
		}

		findAllTeacher(teacherArray);
		// 键盘录入要删除的老师学号,显示提示信息
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你要删除的教师的学号：");
		int id = sc.nextInt();
		// 在删除/修改教师操作前，对教师编号是否存在进行判断
		// 如果不存在，显示提示信息
		// 如果存在，执行删除/修改操作
		int index = -1;
		for (int i = 0; i < teacherArray.size(); i++) {
			Teacher t = teacherArray.get(i);
			if (t.getId() == id) {
				// int index = i;
				index = i; // 把集合的索引赋值给 变量
				break;
			}
		}
		if (index == -1) {
			System.out.println("该信息不存在，请重新输入");
		} else {
			teacherArray.remove(index); // 根据索引删除集合的元素
			// 给出删除成功提示
			System.out.println("删除老师成功");
			findAllTeacher(teacherArray);
			System.out.println("----------------------------------------");
			System.out.println("----------------------------------------");
		}

	}

	public static void addStudent(ArrayList<Student> array) {
		System.out.println("输入学生信息并用,分隔: 编号,姓名,性别,生日,分数,班级,专业");
		// 键盘录入学生对象所需要的数据,显示提示信息，提示要输入何种信息
		Scanner sc = new Scanner(System.in);

		String[] strings = sc.nextLine().split(",");
		if (strings.equals(" ")) {
			System.out.println("请输入信息");
		} else {
			int id = Integer.parseInt(strings[0]);
			String name = strings[1];
			String gender = strings[2];
			String bir = strings[3];
			int grade = Integer.parseInt(strings[4]);
			int classes = Integer.parseInt(strings[5]);
			String field = strings[6];
			Student student = new Student(id, name, gender, bir, grade, classes, field);
			array.add(student);
			System.out.println("添加学生成功");
			// 调用查询所有方法
			findAllStudent(array);
			System.out.println("----------------------------------------");
			System.out.println("----------------------------------------");
		}
	}

	public static void addTeacher(ArrayList<Teacher> teacherArray) {
		System.out.println("输入教师信息并用,分隔: 编号,姓名,性别,生日,部门,职称,工资");
		// 键盘录入老师对象所需要的数据,显示提示信息，提示要输入何种信息
		Scanner sc = new Scanner(System.in);

		String[] strings = sc.nextLine().split(",");
		if (strings.equals(" ")) {
			System.out.println("请输入信息");
		} else {
			int id = Integer.parseInt(strings[0]);
			String name = strings[1];
			String gender = strings[2];
			String bir = strings[3];
			String department = strings[4];
			String job = strings[5];
			double salary = Double.parseDouble(strings[6]);
			Teacher teacher = new Teacher(id, name, gender, bir, department, job, salary);
			teacherArray.add(teacher);
			System.out.println("添加老师成功");
			// 调用查询所有方法
			findAllTeacher(teacherArray);
			System.out.println("----------------------------------------");
			System.out.println("----------------------------------------");
		}
	}

	// 定义一个方法，用于查看学生信息
	public static void findAllStudent(ArrayList<Student> array) {
		// 判断集合中是否有数据，如果没有显示提示信息
		if (array.size() == 0) {
			System.out.println("无信息，请先添加信息再查询");
			// 为了让程序不在往下执行，给出return;
			return;
		}
		for (int i = 0; i < array.size(); i++) {
			Student s = array.get(i);
			s.show();
		}
		System.out.println("----------------------------------------");
		System.out.println("----------------------------------------");
	}

	// 定义一个方法，用于查看学生信息
	public static void findAllTeacher(ArrayList<Teacher> teacherArray) {
		// 判断集合中是否有数据，如果没有显示提示信息
		if (teacherArray.size() == 0) {
			System.out.println("无信息，请先添加信息再查询");
			// 为了让程序不在往下执行，给出return;
			return;
		}
		for (int i = 0; i < teacherArray.size(); i++) {
			Teacher t = teacherArray.get(i);
			t.show();
		}
		System.out.println("----------------------------------------");
		System.out.println("----------------------------------------");
	}

	public static boolean checkStudent(ArrayList<Student> array) {
		if (array == null || array.size() == 0) {
			return false;
		}
		return true;
	}

	public static boolean checkTeacher(ArrayList<Teacher> teacherArray) {
		if (teacherArray == null || teacherArray.size() == 0) {
			return false;
		}
		return true;
	}

}
